*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics

*** Figure A4-2: Effects of prior immigration attitude on vote choice and party identity
*******************************************************************************************

use "DATA_wide.dta", clear

*** Figure A4-2, left panel

	*Regressions for left panel of Figure A4-2 --- Dependent variable: Moves toward and from the CDU/CSU

	matrix w1w7 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w9 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w10 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w17 = J(4,3,.) // 5 rows, 3 columns
	
	logit from1_wunion7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[1, 1] = r[1,1]
	matrix w1w7[1, 2] = r[5,1]
	matrix w1w7[1, 3] = r[6,1]
	
	logit toward1_wunion7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[2, 1] = r[1,1]
	matrix w1w7[2, 2] = r[5,1]
	matrix w1w7[2, 3] = r[6,1]
	
	logit from1_iunion7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[3, 1] = r[1,1]
	matrix w1w7[3, 2] = r[5,1]
	matrix w1w7[3, 3] = r[6,1]
	
	logit toward1_iunion7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[4, 1] = r[1,1]
	matrix w1w7[4, 2] = r[5,1]
	matrix w1w7[4, 3] = r[6,1]
	
	matrix list w1w7
	
	foreach i in 9 10 17 {
	logit from7_wunion`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[1, 1] = r[1,1]
	matrix w7w`i'[1, 2] = r[5,1]
	matrix w7w`i'[1, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit toward7_wunion`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[2, 1] = r[1,1]
	matrix w7w`i'[2, 2] = r[5,1]
	matrix w7w`i'[2, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit from7_iunion`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[3, 1] = r[1,1]
	matrix w7w`i'[3, 2] = r[5,1]
	matrix w7w`i'[3, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit toward7_iunion`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	mat r=r(table)
	matrix w7w`i'[4, 1] = r[1,1]
	matrix w7w`i'[4, 2] = r[5,1]
	matrix w7w`i'[4, 3] = r[6,1]
	matrix list w7w`i'	
	}

	*** Create Figure A4-2, left panel
	coefplot (matrix(w1w7[,1]), ci((w1w7[,2] w1w7[,3])) msymbol(circle) mcolor(cranberry*2) msize(large) ciopts(recast(rbar) barwidth(0.03) color(cranberry*2)) offset(0.15)) ///
			  (matrix(w7w9[,1]), ci((w7w9[,2] w7w9[,3])) msymbol(circle) mcolor(edkblue) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue)) offset(0.05)) ///
			 (matrix(w7w10[,1]), ci((w7w10[,2] w7w10[,3])) msymbol(circle) mcolor(edkblue*.7) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.7)) offset(-.05)) ///
			 (matrix(w7w17[,1]), ci((w7w17[,2] w7w17[,3])) msymbol(circle) mcolor(edkblue*.4) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.4)) offset(-0.15)) ///
			,scheme(plottigblind) /// 
			coeflabels(r1="{&Delta} vote choice, away" r2="{&Delta} vote choice, toward" r3="{&Delta} ID, away" r4="{&Delta} ID, toward" ,labsize(large) labcolor(gs0)) ///
			xscale(range(-.2 .8)) xlabel(-.2 (.2) .8, noticks labsize(large) labcolor(gs0))  xmtick(none) ///
			legend(order(1 "w1-w4" 3 "w4-w5" 5 "w4-w6" 7 "w4-w10") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("CDU/CSU", size(large) color(gs0)) ///
			name(FigA4_2a, replace)

	
*** Figure A4-2, right panel

	*Regressions for right panel of Figure A4-2 --- Dependent variable: Moves toward and from the AfD
		
	matrix w1w7 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w9 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w10 = J(4,3,.) // 5 rows, 3 columns 
	matrix w7w17 = J(4,3,.) // 5 rows, 3 columns
	
	logit from1_wafd7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[1, 1] = r[1,1]
	matrix w1w7[1, 2] = r[5,1]
	matrix w1w7[1, 3] = r[6,1]
	
	logit toward1_wafd7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[2, 1] = r[1,1]
	matrix w1w7[2, 2] = r[5,1]
	matrix w1w7[2, 3] = r[6,1]
	
	logit from1_iafd7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[3, 1] = r[1,1]
	matrix w1w7[3, 2] = r[5,1]
	matrix w1w7[3, 3] = r[6,1]
	
	logit toward1_iafd7 immi1 diff1_immi7
	margins, dydx(immi1 diff1_immi7)
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[4, 1] = r[1,1]
	matrix w1w7[4, 2] = r[5,1]
	matrix w1w7[4, 3] = r[6,1]

	foreach i in 9 10 17 {
	logit from7_wafd`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[1, 1] = r[1,1]
	matrix w7w`i'[1, 2] = r[5,1]
	matrix w7w`i'[1, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit toward7_wafd`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[2, 1] = r[1,1]
	matrix w7w`i'[2, 2] = r[5,1]
	matrix w7w`i'[2, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit from7_iafd`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[3, 1] = r[1,1]
	matrix w7w`i'[3, 2] = r[5,1]
	matrix w7w`i'[3, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 9 10 17 {
	logit toward7_iafd`i' immi7 diff7_immi`i'
	margins, dydx(immi7 diff7_immi`i')
	mat r=r(table)
	matrix w7w`i'[4, 1] = r[1,1]
	matrix w7w`i'[4, 2] = r[5,1]
	matrix w7w`i'[4, 3] = r[6,1]
	matrix list w7w`i'	
	}
	
	*** Create Figure A4-2, right panel
	coefplot (matrix(w1w7[,1]), ci((w1w7[,2] w1w7[,3])) msymbol(circle) mcolor(cranberry*2) msize(large) ciopts(recast(rbar) barwidth(0.03) color(cranberry*2)) offset(0.15)) ///
			  (matrix(w7w9[,1]), ci((w7w9[,2] w7w9[,3]))msymbol(circle) mcolor(edkblue) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue)) offset(0.05)) ///
			 (matrix(w7w10[,1]), ci((w7w10[,2] w7w10[,3])) msymbol(circle) mcolor(edkblue*.7) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.7)) offset(-.05)) ///
			 (matrix(w7w17[,1]), ci((w7w17[,2] w7w17[,3])) msymbol(circle) mcolor(edkblue*.4) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.4)) offset(-0.15)) ///
			,scheme(plottigblind) /// 
			coeflabels(r1="{&Delta} vote choice, away" r2="{&Delta} vote choice, toward" r3="{&Delta} ID, away" r4="{&Delta} ID, toward", labsize(large)) ///
			xscale(range(-.2 .8)) xlabel(-.2 (.2) .8, noticks labsize(large) labcolor(gs0))  xmtick(none) ///
			legend(order(1 "w1-w4" 3 "w4-w5" 5 "w4-w6" 7 "w4-w10") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  ///
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("AfD", size(large) color(gs0)) ///
			name(FigA4_2b, replace)
